<?php namespace app\admin\controller;
header("Content-Type:text/html; charset=utf-8");
use app\common\controller\AdminBase;
use app\common\model\Category;
use app\common\model\Article;
use think\Controller;

//栏目添加管理
class Lans extends AdminBase{
	private $db;
	
	public function _initialize(){
		parent::_initialize();
		$this->db=new Article();
					//子栏目cid
		$cid=input('param.cid');
		$this->assign('cid',$cid);
		//顶级栏目
		$cname=input('param.cname');
		$this->assign('cname',$cname);
		//子栏目
		$name=input('param.name');
		$this->assign('name',$name);
			
	}
	
	//文章列表
	public function index(){
		  // echo "<meta http-equiv='Content-Type'' content='text/html; charset=gb2312'>";
		
		    $cid=input('param.cid');
			$this->assign('cid',$cid);

			//顶级栏目
			$cname=input('cname');
			$this->assign('cname',$cname);
		
			// var_dump($cname);
			//子栏目
			$name=input('name');
			$this->assign('name',$name);
			// halt($name);
			$data=db('article')->where('cid',$cid)->select();
			foreach($data as $k=>$v){
				$data[$k]['cname']=db('category')->where('cid',$v['cid'])->value('cname');
			}
			// halt($data);
			$this->assign('data',$data);
//			var_dump($cid);	var_dump($cname);
//			echo '<br/>';
//			halt($name);
			return $this->fetch();
		
		
	}
	public function news(){
		$data=input('get.');
		halt($data);
	}
	
	
	//添加文章
	public function add(){
		       $cid=input('param.cid');
				$this->assign('cid',$cid);
//				halt($cid);
				//顶级栏目
				$cname=input('param.cname');
				$this->assign('cname',$cname);
				//子栏目
				$name=input('param.name');
				$this->assign('name',$name);
		
		if(request()->isPost()){
			    $data=input('post.');
//			    halt($data);
				$res=$this->db->addData($data);
				if($res['valid']){
					$this->success($res['msg'],url('lans/index',['cname'=>$cname,'name'=>$name]));exit;
//                $this->redirect('lans/news',['cate_id' => 2],['data' => 'hello']);
					
				}else{
					$this->error($res['msg']);exit;
				}
		}else{
			//获得栏目数据
				$data=db('category')->where('cid',$cid)->find();
//				halt($data);
				$this->assign('data',$data);
				
				return $this->fetch();
		}
		
	}
	
	
		//栏目编辑
	public function edit(){
		if(request()->isPost()){
//		halt(input('post.'));
			$res=$this->db->editData(input('post.'));
//			halt(input('post.'));
			if($res['valid']){
				
				$this->success($res['msg'],'index');exit;
				
			}else{
				
				$this->error($res['msg']);exit;
			}
		}else{
			
			$aid=input('aid');
		
			//获得对应的数据
			$oneData=db('article')->where('aid',$aid)->find();
//			
			$oneData['cname']=db('category')->where('cid',$oneData['cid'])->value('cname');
//			}
			$this->assign('oneData',$oneData);
//			halt($oneData);
//         调用找子类的方法
            
	    	return $this->fetch();
			
		}
	
	}
	//删除
	public function delete(){
		  $aid=input('aid');
		 
//	    halt($aid);
//	    echo count($cids);exit;
          $path=db('article')->where('aid',$aid)->value('thumb');
          
        
	      //删除pic表的数据
	      if(!empty($path)){
		           $new=substr($path,strpos($path,'public'));
	//	      halt($new);
		      // $www= substr(__FILE__,0,strpos(__FILE__,'app'));//G:\wamp\www\tp_5.0.5\
			  
			  // $ben=$www.str_replace('/','\\',$new);
	//		  echo $ben;die;
			  //删除本地的图片
		       if(file_exists($new)){ //检查图片文件是否存在
				    unlink($new); 
				  
			    }
			    db('pic')->where('path',$new)->delete();
		    
		 }   
	     //删除文章表的数据
	    	$num=db('article')->delete($aid);
//	    	echo $num;exit;
	    	if($num){
	    		
	    		$this->success('删除成功！');
	    	}else{
	    		$this->error('删除失败！');
	    	}
	    	
	  
	}
	
	//异步删除图片
	public function del(){
		
		$path=input('path');
	
		if(!empty($path)){
			
	      $new=substr($path,strpos($path,'u'));
//	      halt($new);
	      $www= substr(__FILE__,0,strpos(__FILE__,'app'));//G:\wamp\www\tp_5.0.5\
		  
		  $ben=$www.str_replace('/','\\',$new);
//		  echo $ben;die;
		  //删除本地的图片
	       if(file_exists($ben)){ //检查图片文件是否存在
			    unlink($ben); 
			  
		    }else{
		     	echo "图片路径不正确";
		    }
		    //删除pic表的数据
			  db('pic')->where('path',$new)->delete();
		}
	
//		echo $path;
	}
	
}
